package com.example.hszpad.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hszpad.R;
import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.adapter.SmartViewHolder;
import com.example.hszpad.bean.MenuItemBean;

import java.util.ArrayList;
import java.util.List;


public class HSZViewFragment extends Fragment {

    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<MenuItemBean> recyclerAdapter;
    private List<MenuItemBean> menuItemBeanList;
    private MenuItemBean curItemBean;

    private HSZViewFragmentPower hszViewFragmentPower = new HSZViewFragmentPower();
    private HSZViewFragmentAssist hszViewFragmentAssist = new HSZViewFragmentAssist();
    private HSZViewFragmentElectric hszViewFragmentElectric = new HSZViewFragmentElectric();
    private HSZViewFragmentHistory hszViewFragmentHistory = new HSZViewFragmentHistory();
    private HSZViewFragmentReal hszViewFragmentReal = new HSZViewFragmentReal();
    private HSZViewFragmentDamage hszViewFragmentDamage = new HSZViewFragmentDamage();
    private HSZViewFragmentWarn hszViewFragmentWarn = new HSZViewFragmentWarn();

    public HSZViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hszview, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        menuItemBeanList = getMenus();
        recyclerAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanList, R.layout.layout_view_list_item, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItemBean itemBean = menuItemBeanList.get(position);
                if (itemBean.isSelect()) {
                    return;
                } else {
                    for (int i = 0; i < menuItemBeanList.size(); i++) {
                        menuItemBeanList.get(i).setSelect(false);
                    }
                    itemBean.setSelect(true);
                    switchContent(itemBean);
                    curItemBean = itemBean;
                }
                recyclerAdapter.notifyDataSetChanged();
                recyclerAdapter.notifyListDataSetChanged();
            }
        }) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
                holder.text(R.id.tv_title, model.getName());
                if (model.isSelect()) {
                    holder.viewBgColorId(R.id.ll_content, R.color.colorAccent);
                    holder.textColorId(R.id.tv_title, R.color.colorPrimary);
                } else {
                    holder.viewBgColorId(R.id.ll_content, R.color.colorPrimary);
                    holder.textColorId(R.id.tv_title, R.color.colorAccent);
                }
            }
        };
        recyclerView.setAdapter(recyclerAdapter);
        switchContent(menuItemBeanList.get(0));
        return view;
    }


    private List<MenuItemBean> getMenus() {
        List<MenuItemBean> menuItemBeanList = new ArrayList<>();
        MenuItemBean bean = new MenuItemBean(1, "动力系统", 0, R.drawable.ic_home_white_24dp, true);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(2, "电力系统", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(3, "辅助设备", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
//        bean = new MenuItemBean(6, "破损抗沉管理", 0, R.drawable.ic_home_white_24dp);
//        menuItemBeanList.add(bean);
        bean = new MenuItemBean(8, "实时数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(9, "历史数据", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        bean = new MenuItemBean(10, "报警列表", 0, R.drawable.ic_home_white_24dp);
        menuItemBeanList.add(bean);
        return menuItemBeanList;
    }

    private void switchContent(MenuItemBean bean) {
        if (bean == null) {
            return;
        }
        if (curItemBean != null && curItemBean == bean) {
            return;
        }
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction tran = fm.beginTransaction();
        switch (bean.getId()) {
            case 1:
                tran.replace(R.id.fl_content, hszViewFragmentPower);
                break;
            case 2:
                tran.replace(R.id.fl_content, hszViewFragmentElectric);
                break;
            case 3:
                tran.replace(R.id.fl_content, hszViewFragmentAssist);
                break;
            case 6:
                tran.replace(R.id.fl_content, hszViewFragmentDamage);
                break;
            case 8:
                tran.replace(R.id.fl_content, hszViewFragmentReal);
                break;
            case 9:
                tran.replace(R.id.fl_content, hszViewFragmentHistory);
                break;
            case 10:
                tran.replace(R.id.fl_content, hszViewFragmentWarn);
                break;
        }
        tran.commit();
    }
}
